package com.ruoyi.app.controller;

import java.util.List;

import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.ruoyi.app.annotation.AppSecurity;
import com.ruoyi.app.util.ReqMsg;
import com.ruoyi.app.util.RspMsg;
import com.ruoyi.bizsys.domain.AreaCode;
import com.ruoyi.bizsys.service.IAreaCodeService;
import com.ruoyi.business.service.IBAreaCodeService;
import com.ruoyi.common.enums.RspEnmu;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.enums.BusinessType;

/**
 * 地区Controller
 *
 * @author african-loan
 * @date 2021-06-29
 */

@Api("地区接口")
@RestController
@Slf4j
@RequestMapping("/address")
public class AddressController extends BaseController {
    @Autowired
    private IAreaCodeService iAreaCodeService;


    @Autowired
    private IBAreaCodeService iBAreaCodeService;

    /**
     * 获取树形结构
     */
    @PostMapping("/getTree")
    @AppSecurity
    @ApiOperation("获取树形结构")
    @Log(title = "获取树形结构", businessType = BusinessType.GET)
    public RspMsg getTree(@RequestBody ReqMsg reqMsg) {
        return RspMsg.ok(iBAreaCodeService.getTree());
    }

    /**
     * 获取子级地区
     */
    @PostMapping("/getForParent")
    @AppSecurity
    @ApiOperation("获取子级地区")
    @Log(title = "获取子级地区", businessType = BusinessType.GET)
    public RspMsg getForParent(@RequestBody ReqMsg reqMsg) {

        //Gson gson = new Gson();
        AreaCode areaCode = JSON.parseObject(reqMsg.getData().toString(), AreaCode.class);
        log.info("请求data：{}", JSON.toJSONString(areaCode));

        if(areaCode.getId()==null){
            return RspMsg.fail(RspEnmu.PARAM_CANNOT_NULL);
        }

        if(areaCode.getId()<0){
            return RspMsg.fail(RspEnmu.ILLEGAL_ARGUMENT);
        }

        AreaCode areaCode1 = new AreaCode();
        areaCode1.setParentId(areaCode.getId());

        List<AreaCode> list = iAreaCodeService.selectAreaCodeList(areaCode1);
        return RspMsg.ok(list);
    }
}
